package com.lazy.book.service.system.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.lazy.book.common.entity.SocketMessage;
import com.lazy.book.common.entity.TransferMessage;
import com.lazy.book.common.enums.CommonResponseEnum;
import com.lazy.book.common.enums.SocketChannelEnum;
import com.lazy.book.common.pojp.LoginInfo;
import com.lazy.book.common.pojp.LoginVO;
import com.lazy.book.common.utils.SpringApplicationContextUtils;
import com.lazy.book.common.entity.ClientVO;
import com.lazy.book.service.system.AuthService;
import com.lazy.book.service.system.ClientService;
import com.lazy.book.service.system.IAuthStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

  private final WebsocketRedisService websocketRedisService;

  @Override
  public LoginVO loginClient(LoginInfo info) {
    String clientId = info.getClientId();
    ClientService clientService = SpringApplicationContextUtils.getInstance().getBean(ClientService.class);
    ClientVO client = clientService.getClientByClientId(clientId);
    // 验证clientId有效性
    CommonResponseEnum.CLIENT_INVALID.assertNull(client);
    // 验证client status 有效性
    CommonResponseEnum.CLIENT_BLOCKED.assertTrue(!("1003001").equals(client.getClientStatusCd()));
    return IAuthStrategy.login(info, client, info.getGrantType());
  }

  @Override
  public LoginVO authLogin(LoginInfo info) {
    ClientService clientService = SpringApplicationContextUtils.getInstance().getBean(ClientService.class);
    ClientVO client = clientService.getClientByClientId("fae1dc944442434dac8870e6a4eb59ef");
    // 验证clientId有效性
    CommonResponseEnum.CLIENT_INVALID.assertNull(client);
    // 验证client status 有效性
    CommonResponseEnum.CLIENT_BLOCKED.assertTrue(!("1003001").equals(client.getClientStatusCd()));
    info.setGrantType("Token");
    return IAuthStrategy.login(info, client, info.getGrantType());
  }

  @Override
  public LoginVO tokenLogin(String token) {
    LoginInfo info = new LoginInfo();
    ClientService clientService = SpringApplicationContextUtils.getInstance().getBean(ClientService.class);
    ClientVO client = clientService.getClientByClientId("fae1dc944442434dac8870e6a4eb59ef");
    // 验证clientId有效性
    CommonResponseEnum.CLIENT_INVALID.assertNull(client);
    // 验证client status 有效性
    CommonResponseEnum.CLIENT_BLOCKED.assertTrue(!("1003001").equals(client.getClientStatusCd()));
    info.setCode(token);
    info.setGrantType("UrlToken");
    return IAuthStrategy.login(info, client, info.getGrantType());
  }

  /**
   * 强制注销指定用户
   *
   * @param id 用户id
   */
  @Override
  public void kickOut(Long id) {
    TransferMessage tm = new TransferMessage();
    tm.setToUsers(Collections.singletonList(id + ""));
    SocketMessage sb = new SocketMessage();
    sb.setChannel(SocketChannelEnum.KICK_OFF);
    tm.setMessage(sb);
    websocketRedisService.sendServiceToWs(tm);
    StpUtil.logout(id);
  }


}

